layui.use(['layer', 'form', 'table', 'ztree', 'laydate','laypage', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var laypage = layui.laypage;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;


    /**
     * 系统管理--用户管理
     */
    var MgrUser = {
        tableId: "userTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 遍历获取当前所有框架
     */
    var count1;
    var pageSize = 1;

    var queryData = {};
    loadFrame();

    /**
     * 遍历获取当前所有框架
     */
    function loadFrame(dataa){
        if (dataa==null){
            dataa ={}
        }
        /**
         * 第一个ajax获取页数
         */
        $.ajax({
            url:Feng.ctxPath + "/frame/frame_list",
            data: dataa,
            dataType:"json",
            success:function(pageMessage){
                laypage.render({
                    elem: 'pageDemo'
                    ,count: pageMessage.count //显示所有条数
                    ,curr: pageSize
                    ,limit: 8
                    ,page: true
                    ,layout: ['count', 'prev', 'page', 'next', 'skip']
                    ,jump: function(obj,first){
                        //设置页面下一页内容
                        dataa['page'] =obj.curr;
                        dataa['limit']= obj.limit;
                        //设置记录
                        pageSize = obj.curr;
                        // jsonData1 = jsonData.concat().splice(obj.curr * obj.limit - obj.limit, obj.limit);
                        // var nextPageMessage =  new $ax(Feng.ctxPath + "/frame/frame_list?page="+obj.curr+"&limit="+obj.limit).start();
                        $.ajax({
                           url: Feng.ctxPath + "/frame/frame_list",
                            data: dataa,
                            dataType:"json",
                            success:function (nextPageMessage) {
                                $('#frameName').empty();
                               if (nextPageMessage.count==0){
                                   var html ='';
                                   html += '<div class="layui-col-lg12 layui-col-md12" style="text-align:center;">';
                                   html += '    <img src="/assets/null.jpg" style="height: 21rem;width: 33rem;">';
                                   html += '</div>';
                                   $('#frameName').append(html);
                               }else {

                                   layui.each(nextPageMessage.data,function (index,item) {
                                       var lock = index + 'lock';
                                       var editId = index + 'edit';
                                       var str='';
                                       str += '<div class="layui-col-xs12 layui-col-sm6 layui-col-md3">';
                                       str += '    <div class="myui-card">';
                                       str += '        <div class="layui-card-header">'+ item.frameName +'<span class="pull-right">';
                                       if (item.status =="ENABLE") {
                                           if (item.postStatus =="DISABLE") {
                                               str += '            <i class="layui-icon layui-icon-log" id=' + lock+'></i>';
                                           }
                                           str += '            <i class="layui-icon layui-icon-upload" id=' + lock+'></i></span></div>';
                                       }else {
                                           if (item.postStatus =="DISABLE") {
                                               str += '            <i class="layui-icon layui-icon-log" id=' + lock+'></i>';
                                           }
                                           str += '            <i class="layui-icon layui-icon-password" id=' + lock+'></i></span></div>';
                                       }
                                       str += '        <div class="layui-card-body">';
                                       str += '            <p class="myFont" id="scheduleName">';
                                       str +=  item.frameDescription!=""?item.frameDescription:"它还没有描述哦～～";
                                       str += '            </p>';
                                       str += '        </div>';
                                       str += '        <ul class="icon">';
                                       str += '            <li id ="viewFrame' + index +'"><a><i class="layui-icon layui-icon-form"></i></a></li>';
                                       str += '            <li id ="editFrame' + index +'"><a><i class="layui-icon layui-icon-edit"></i></a></li>';
                                       if(item.postStatus =="DISABLE"){
                                           str += '            <li id ="releFrame' + index +'"><a><i class="layui-icon layui-icon-share"></i></a></li>';
                                       }
                                       str += '            <li id ="deleteFrame' + index +'"><a><i class="layui-icon layui-icon-delete"></i></a></li>';
                                       str += '        </ul>';
                                       str += '    </div>';
                                       str += '</div>';

                                       $('#frameName').append(str);

                                       /**
                                        * 查看框架内容
                                        */
                                       $('#viewFrame'+index).click(function () {
                                           layer.open({
                                               type: 2,
                                               title:'查看'+item.frameName+'框架',
                                               area: ['90%', '95%'],
                                               fixed: false, //不固定
                                               maxmin: true,
                                               content: Feng.ctxPath + '/frame/view?frameId=' + item.frameId,
                                               success:function () {
                                               }
                                           });
                                       });

                                       /**
                                        * 保存后编辑框架
                                        */
                                       $('#editFrame'+index).click(function () {
                                           layer.open({
                                               type: 2,
                                               title:'编辑'+item.frameName+'框架',
                                               area: ['90%', '95%'],
                                               fixed: false, //不固定
                                               maxmin: true,
                                               content: Feng.ctxPath + '/frame/edit?frameId=' + item.frameId,
                                               success:function () {
                                               },
                                               end : function() {
                                                   loadFrame(queryData);
                                               }
                                           });
                                       });

                                       /**
                                        * 发布框架
                                        */
                                       $('#releFrame' +index).click(function () {
                                           var operation = function () {
                                               var ajax = new $ax(Feng.ctxPath + "/frame/releaseFrame", function () {
                                                   table.reload(MgrUser.tableId);
                                                   Feng.success("发布成功!");
                                                   //回调当前页面
                                                   loadFrame(dataa);
                                               }, function (data) {
                                                   Feng.error("发布失败!" + data.responseJSON.message + "!");
                                               });
                                               ajax.set("frameId", item.frameId);
                                               ajax.start();
                                               //刷新当前页面
                                               // location.reload();
                                           };
                                           Feng.confirm("是否发布"+item.frameName+"框架?发布后内容不可修改", operation);
                                       });

                                       /**
                                        * 删除框架
                                        */
                                       $('#deleteFrame' +index).click(function () {
                                           var operation = function () {
                                               var ajax = new $ax(Feng.ctxPath + "/frame/deleteFrame", function () {
                                                   table.reload(MgrUser.tableId);
                                                   Feng.success("删除成功!");
                                                   //刷新当前页面
                                                   // location.reload();
                                                   //回调当前页面
                                                   loadFrame(dataa);
                                               }, function (data) {
                                                   Feng.error("删除失败!" + data.responseJSON.message + "!");
                                               });
                                               ajax.set("frameId", item.frameId);
                                               ajax.start();

                                           };
                                           Feng.confirm("是否删除"+item.frameName+"框架?", operation);
                                       });
                                   });
                               }
                            }
                        });
                    }
                });
            }
        });


        // var pageMessage = new $ax(Feng.ctxPath + "/frame/frame_list",dataa,"json").start();

    }


    /**
     * 获取公开或自己的的框架
     */
    form.on('select(otherOrMyself)', function(data) {
        queryData['otherOrmyself'] = data.value;
        pageSize =1;
        queryData['page'] = 1;
        loadFrame(queryData);
    });


    /**
     * 点击查询按钮
     */
    MgrUser.search = function () {

        queryData['otherOrmyself'] = $("#otherOrmyself").val();
        queryData['nameOrcontent'] = $("#nameOrcontent").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        pageSize =1;
        queryData['page'] = 1;
        // alert(JSON.stringify(queryData));
        loadFrame(queryData);

        // $.ajax(Feng.ctxPath + "/frame/frame_list",queryData.nameOrcontent)
        // table.reload(MgrUser.tableId, {
        //     where: queryData, page: {curr: 1}
        // });
    };

    /**
     * 弹出创建日程框架
     */
    MgrUser.openAddUser = function () {
        // func.open({
        //     title: '创建日程框架',
        //     content: Feng.ctxPath + '/frame/create',
        //     tableId: MgrUser.tableId
        // });
        layer.open({
            type: 2,
            title:'创建日程框架',
            area: ['90%', '95%'],
            fixed: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/frame/create'
        });
    };




    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });



    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MgrUser.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MgrUser.openAddUser();
    });


    // 发布框架
    $('#btnReleFrame').click(function () {
        MgrUser.releFrame();
    });

});

